﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TakeoutExpress.Support;
using TakeoutExpress.Support.Controllers;

namespace TakeoutExpress2008.Web.MVC.UserControls
{
    public partial class SiteDashboardView : Components.BaseControl
    {

        public enum DashboardViewsEnum
        {
            AllSites = 1,
            MySites = 2,
        }

        public event GridViewCommandEventHandler SiteViewRowCommand;

        protected virtual void OnSiteViewCommand(GridViewCommandEventArgs e)
        {
            if (SiteViewRowCommand != null)
                SiteViewRowCommand(this, e);
            
        }

        protected bool DisplayJoin
        {
            get
            {
                return DashboardView == DashboardViewsEnum.AllSites;
            }
        }
        protected bool DisplayUnjoin
        {
            get
            {
                return DashboardView == DashboardViewsEnum.MySites;
            }
        }
        private DashboardViewsEnum _dashboardView = DashboardViewsEnum.AllSites;

        public DashboardViewsEnum DashboardView
        {
            get
            {
                return _dashboardView;
            }
            set
            {
                _dashboardView = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            gvSitesView.RowCommand += new GridViewCommandEventHandler(gvSitesView_RowCommand);
            gvSitesView.Columns[1].Visible = DisplayJoin;
            gvSitesView.Columns[2].Visible = DisplayUnjoin;
            
            //Login loginControl = (Login) Page.Master.FindControl("Login1");

                      
            if (!Page.IsPostBack)
            {
                PopulateGrid();
             
            }
        }

        void mp_LoggedIn(object sender, EventArgs e)
        {
            PopulateGrid();
        }

        void loginControl_LoggedIn(object sender, EventArgs e)
        {
            PopulateGrid();
        }


        public void PopulateGrid()
        {
            SiteController siteController = new SiteController();
            switch (DashboardView)
            {
                case DashboardViewsEnum.AllSites:
                    gvSitesView.DataSource = siteController.GetUnsubscribedSites(Page.User.Identity.Name);
                    gvSitesView.DataKeyNames = new string[] { "SiteID" };
                    gvSitesView.DataBind();
                    break;

                case DashboardViewsEnum.MySites:
                    gvSitesView.DataSource = siteController.GetSubscribedSites(Page.User.Identity.Name);
                    gvSitesView.DataKeyNames = new string[] { "SiteID" };
                    gvSitesView.DataBind();
                    break;

                default:
                    _dashboardView = DashboardViewsEnum.AllSites;
                    break;
            }
        }
        void gvSitesView_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            SiteController siteController = new SiteController();
            int rowNumber, key;
            switch (e.CommandName.ToLower())
            {
                case "join":
                    rowNumber = int.Parse(e.CommandArgument.ToString());
                     key = (int)gvSitesView.DataKeys[rowNumber].Value;
                    siteController.SubscribeToSite(Page.User.Identity.Name, key);
                    PopulateGrid();
                    OnSiteViewCommand(e); // bubble up
                
                    break;
                case "unjoin":
                    rowNumber = int.Parse(e.CommandArgument.ToString());
                    key = (int)gvSitesView.DataKeys[rowNumber].Value;
                    siteController.UnsubscribeFromSite(Page.User.Identity.Name, key);
                    PopulateGrid();
                    OnSiteViewCommand(e);
                    break;

                default: 
                    break;



            }
        }
    }
}